package com.xhu.rjsx2.controller;

import com.xhu.rjsx2.enums.AppHttpCodeEnum;
import com.xhu.rjsx2.pojo.Coach;
import com.xhu.rjsx2.pojo.ResponseResult;
import com.xhu.rjsx2.pojo.dto.CoachDTO;
import com.xhu.rjsx2.pojo.vo.CoachVO;
import com.xhu.rjsx2.pojo.vo.LayuiPageResult;
import com.xhu.rjsx2.pojo.vo.PageVO;
import com.xhu.rjsx2.service.CoachService;
import com.xhu.rjsx2.util.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/coach")
@RestController
@Api(tags = "教练（coach）管理接口API",description = "Coach Controller")
public class CoachController {

    @Resource
    private CoachService coachService;

    @GetMapping("/listAllCoach")
    @ApiOperation(value = "查询所有教练")
    public ResponseResult listAllCoach(){
        List<Coach> coachList = coachService.list();
        List<CoachVO> coachVOList = BeanCopyUtils.copyBeanList(coachList, CoachVO.class);
        return ResponseResult.okResult(coachVOList);
    }

    @GetMapping("/listAllCoachPage")
    @ApiOperation(value = "分页查询所有教练")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数"),
            @ApiImplicitParam(name = "limit", value = "每页条数"),
            @ApiImplicitParam(name = "coachName", value = "教练姓名"),
            @ApiImplicitParam(name = "coachHeight", value = "教练身高(整数cm)"),
            @ApiImplicitParam(name = "coachWeight", value = "体重")
    })
    public LayuiPageResult listAllCoachPage(Integer page,Integer limit,String coachName,Integer coachHeight,Integer coachWeight){

        PageVO pageVO = coachService.listAllCoachPage(page,limit,coachName,coachHeight,coachWeight);
        return new LayuiPageResult(LayuiPageResult.CODE_SUCCESS,LayuiPageResult.MESSAGE_SUCCESS,pageVO.getRows(), pageVO.getTotal());
    }


    @PostMapping("/updateCoachById")
    @ApiOperation(value = "通过主键更新教练信息")
    public ResponseResult updateMemberById(@RequestBody CoachDTO coachDTO){
        Coach coach = BeanCopyUtils.copyBeanSingle(coachDTO, Coach.class);
        boolean updateSuccess = coachService.updateById(coach);
        return updateSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }


    @PostMapping("/deleteCoachById")
    @ApiOperation(value = "通过主键删除教练信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "当前教练id")
    })
    public ResponseResult deleteCoachById(Integer id){
        boolean removeSuccess = coachService.removeById(id);
        return removeSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }


    @PostMapping("/insertCoach")
    @ApiOperation(value = "新增教练接口")
    public ResponseResult insertCoach(@RequestBody CoachDTO coachDTO){
        // 由系统生成自增id
        coachDTO.setId(null);
        coachDTO.setHeader("http://rued9prih.hn-bkt.clouddn.com/FoOp9CxNmERSrimeQjp8MgFxagw-");
        Coach coach = BeanCopyUtils.copyBeanSingle(coachDTO, Coach.class);
        boolean saveSuccess = coachService.save(coach);
        return saveSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}
